<template>
  <div class="card-container">
    <!-- 派单弹窗 -->
    <el-dialog title="警告派单" :visible.sync="dialogFormVisible">
      <el-form ref="idList" :model="idList" :rules="rule">
        <el-form-item label="处理人" prop="userId" :label-width="formLabelWidth">
          <el-select class="el-sel1" v-model="idList.userId" placeholder="请选择处理人">
            <el-option label="amdin" :value='0'></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="poleDispatch(idList.userId)">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">一体杆名称：</span>
      <el-input clearable placeholder="请输入一体杆名称：" @clear="getList" v-model="params.poleName" class="search-main" />
      <span class="search-label">一体杆编号：</span>
      <el-input clearable placeholder="请输入一体杆编号：" @clear="getList" v-model="params.poleNumber" class="search-main" />
      <span class="search-label">运行状态：</span>
      <el-select v-model="params.handleStatus">
        <el-option label="全部" :value=null></el-option>
        <el-option label="未派单" :value="0"></el-option>
        <el-option label="已派单" :value="1"></el-option>
        <el-option label="已接单" :value="2"></el-option>
        <el-option label="已完成" :value="3"></el-option>
      </el-select>
      <el-button type="primary" class="search-btn" @click="getList">查询</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="list" style="width: 100%">
        <el-table-column type="index" label="序号" />
        <el-table-column label="一体杆名称" prop="poleName" />
        <el-table-column label="一体杆编号" prop="poleNumber" />
        <el-table-column label="故障类型" prop="errorType" />
        <el-table-column :formatter="fn" label="处置状态" prop="handleStatus" />
        <el-table-column label="告警时间" prop="warningTime" />
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="scope">
            <el-button size="mini" type="text" :disabled="isButtonDisabled(scope.row)"
              @click="open(scope.row.id)">派单</el-button>
            <el-button size="mini" type="text" @click="jump(scope.row.id)">详情</el-button>
            <el-button size="mini" type="text" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="page-container">
      <el-pagination layout="total,sizes, prev, pager, next" :current-page="params.page" :page-sizes="[2, 4, 6, 8, 10]"
        :page-size="params.pageSize" :total="total" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </div>
  </div>
</template>

<script>
//请求接口 正常绝大多数情况下的都是正常响应数据 响应状态码200
//极少数情况下不会响应数据 响应状态码除了200
//401 未授权(不认识)
// import { getMonthCardListAPI } from '@/api/car'
import { getpoleWarningListAPI, delpoleWarningAPI, poleDispatchAPI, } from '@/api/warning'
export default {
  data() {
    return {
      //提供列表数据
      list: [],
      total: 0,
      dialogFormVisible: false,
      formLabelWidth: '120px',
      params: {
        page: 1,
        pageSize: 10,
        poleName: null,
        poleNumber: null,
        handleStatus: null
      },
      idList: {
        id: 0,
        userId: null,
      },
      rule: {
        userId: [
          { required: true, message: '请选择处理人', trigger: 'blur' }
        ],
      }
    }
  },
  mounted() {
    //这是一个生命周期函数(钩子函数)在特定时期执行的
    this.getList()
  },
  methods: {
    jump(id) {
      this.$router.push('/details?id=' + id)
    },
    fn(row) {
      const sss = null
      if (row.handleStatus == 0) { return '未派单' }
      if (row.handleStatus == 1) { return '已派单' }
      if (row.handleStatus == 2) { return '已接单' }
      if (row.handleStatus == 3) { return '已完成' }
    },
    //获取列表
    getList() {
      //一定是请求结束之后才会执行
      //响应回来的数据 就是res参数
      getpoleWarningListAPI(this.params).then((res) => {
        this.list = res.data.rows
        this.total = res.data.total
      })
    },
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize//更新每页条数
      this.getList()
    },
    handleCurrentChange(page) {//更新页数
      this.params.page = page
      this.getList()
    },
    //删除
    del(id) {
      this.$confirm("是否确定删除").then(() => {
        delpoleWarningAPI(id).then(() => {
          this.getList()
          this.$message.success('删除成功')
        }).catch((error) => {
          this.$message.error(error.response.data.msg)
          this.getList()
        })
      })
    },
    open(id) {
      this.idList.id = id
      this.dialogFormVisible = true
    },
    close(){
      this.dialogFormVisible = false
      this.idList.id = 0
      this.idList.userId = null
    },
    //派单
    poleDispatch(userId) {
      this.$refs.idList.validate(async (valid) => {
        if (valid) {
          const data = { id: this.idList.id, userId: userId }
          poleDispatchAPI(data).then(() => {
            this.$message.success("操作成功")
            this.close()
            this.getList()
          }).catch((error) => {
            this.$message.error(error.response.data.msg)
            this.close()
          })
          this.idList.id = 0
          this.idList.userId = null
        }
      })
    },
    isButtonDisabled(row) {
      if (row.handleStatus == 2 || row.handleStatus == 1) return true
      else { return false }
    }
  },

}
</script>


<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}


.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-main {
    width: 220px;
    margin-right: 10px;
  }

  .search-btn {
    margin-left: 20px;
  }
}

.create-container {
  margin: 10px 0px;
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
}

.el-put1 {
  width: 400px;
}

.el-sel1 {
  width: 400px;
}
</style>
